﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.Procurement
{
    public class PaymentPlansCreateHandler : IRequestHandler<PaymentPlansCreateCommand, Result>
    {
        /// <summary>
        /// 采购/委外订单头信息
        /// </summary>
        private readonly IBaseRepository<PaymentPlans> PaymentPlansRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<PaymentPlansCreateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasingsRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        public PaymentPlansCreateHandler(IBaseRepository<PaymentPlans> PaymentPlansRepository, IMapper mapper,  ILogger<PaymentPlansCreateHandler> logger)
        {
            this.PaymentPlansRepository = PaymentPlansRepository;
            this.mapper = mapper;
            this.logger = logger;
        }
        public async Task<Result> Handle(PaymentPlansCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Update };

            try
            {
                var AddData = mapper.Map<PaymentPlans>(request);
                var Sql = "insert into PaymentPlans values(@PaymentPlanId,@PaymentPlanDate,@ProportionPayment,@PaymentPrice,@PaymentAdvance,@Remarks,@PurchasinId,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
                var AddNum = PaymentPlansRepository.Add(Sql, new
                {
                    PaymentPlanId = AddData.PaymentPlanId,
                    PaymentPlanDate = AddData.PaymentPlanDate,
                    ProportionPayment = AddData.ProportionPayment,
                    PaymentPrice = AddData.PaymentPrice,
                    PaymentAdvance = AddData.PaymentAdvance,
                    Remarks = AddData.Remarks,
                    PurchasinId = AddData.PurchasinId,
                    Handle = request.Handle,
                    HandleContext = request.HandleContext,
                    HandleTime = DateTime.Now,
                    Pid = request.Pid,
                    IsDelete = AddData.IsDelete
                });
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = null;
                    result.Msg = "付款计划添加成功";
                    logger.LogInformation("付款计划添加成功");
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Update;
                    result.Data = null;
                    result.Msg = "付款计划添加失败";
                    logger.LogError("付款计划添加失败，");

                }
            }
            catch (Exception ex)
            {
                logger.LogError($"付款计划添加失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
